package com.cn.web.controller.mc;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.pojo.mc.K3Dy;
import com.cn.bean.pojo.mc.K3Fl;
import com.cn.bean.pojo.mc.K3FlXx;
import com.cn.bean.vo.mc.K3WhVo;
import com.cn.config.config.Result;
import com.cn.service.server.bc.MaterialServer;
import com.cn.service.server.mc.K3WhServer;
import com.cn.service.server.mc.K3WlXxSQServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("/MC")
public class K3WlXxSQController {
    private final K3WlXxSQServer k3WlXxSQServer;
    private final MaterialServer materialServer;

    @Autowired
    public K3WlXxSQController(K3WlXxSQServer k3WlXxSQServer, MaterialServer materialServer) {
        this.k3WlXxSQServer = k3WlXxSQServer;
        this.materialServer = materialServer;
    }

    @RequestMapping("/Select/K3WlXxSQ")
    @ResponseBody
    public List<K3FlXx> SelectK3WlXxSQ(@RequestBody JSONObject jsonObject) {
        int page = jsonObject.getIntValue("page");
        int limit = jsonObject.getIntValue("limit");
        String djBh = jsonObject.getString("djBh");
        String k3Mc = jsonObject.getString("k3Mc");
        String lx = jsonObject.getString("lx");
        lx = (lx != null) ? lx : "";
        djBh = (djBh != null) ? djBh : "";
        k3Mc = (k3Mc != null) ? k3Mc : "";
        List<K3FlXx> k3FlXxes = k3WlXxSQServer.selectK3FlXxSQAll(page, limit, djBh, k3Mc, lx);
        return k3FlXxes;
    }

    @RequestMapping("/upDate/K3WlXx/ShZt")
    @ResponseBody
    public Result upDateK3FlXxShZt(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("djBh");
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<String> djBhList = djBhArray.toJavaList(String.class);
            for (String djBh : djBhList) {
                k3WlXxSQServer.upDateK3FlXxShZt(djBh, userName, name, bmmc, currentFormattedDateTime);
            }
            // 返回成功的响应
            return Result.success("200", "提交成功");
        }
        return Result.failure("500", "提交失败");
    }

    @RequestMapping("/upDate/K3WlXx/ShZt1")
    @ResponseBody
    public Result upDateK3FlXxShZt1(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("djBh");
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<String> djBhList = djBhArray.toJavaList(String.class);
            for (String djBh : djBhList) {
                K3FlXx k3FlXx = k3WlXxSQServer.selectK3FlXxDjBh(djBh);
                int isScMx = k3FlXx.getIsScMx();
                if (isScMx==1){
                    isScMx = 2;
                }else {
                    isScMx=7;
                }
                k3WlXxSQServer.upDateK3FlXxShZt1(isScMx,djBh, userName, name, bmmc, currentFormattedDateTime);
            }
            // 返回成功的响应
            return Result.success("200", "提交成功");
        }
        return Result.failure("500", "提交失败");
    }

    @RequestMapping("/upDate/K3WlXx/ShZt10")
    @ResponseBody
    public Result upDateK3FlXxShZt10(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("djBh");
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<String> djBhList = djBhArray.toJavaList(String.class);
            for (String djBh : djBhList) {
                k3WlXxSQServer.upDateK3FlXxShZt10(djBh, userName, name, bmmc, currentFormattedDateTime);
            }
            // 返回成功的响应
            return Result.success("200", "提交成功");
        }
        return Result.failure("500", "提交失败");
    }

    @RequestMapping("/add/K3WlXx/Inseart")
    @ResponseBody
    public Result addK3FlXxInseart(@RequestBody JSONObject jsonObject) {
        K3FlXx k3FlXx = jsonObject.getObject("K3FlXx", K3FlXx.class);
        Integer k3WlXxSQDdBhZh = k3WlXxSQServer.getK3WlXxSQDdBhZh(k3FlXx.getDjBh(), k3FlXx.getZh());
        Integer k3WlXxSQDdBhZh1 = k3WlXxSQServer.getK3WlXxSQDdBhZh(k3FlXx.getDjBh(), "");
        if (!Objects.equals(k3WlXxSQDdBhZh, 0)) {
            return Result.failure("500", "单件编号重复");
        }
        if (!Objects.equals(k3WlXxSQDdBhZh1, 0)) {
            return Result.failure("500", "单件编号重复");
        }
        String lySj = jsonObject.getString("lySj");
        lySj = lySj.replace("/", "-");
        String userName = jsonObject.getString("userName");
        String lx = jsonObject.getString("lx");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        k3FlXx.setLyrGh(userName);
        k3FlXx.setLyrXm(name);
        k3FlXx.setLyrBm(bmmc);
        k3FlXx.setLySj(lySj);
        k3FlXx.setZt(1);
        k3FlXx.setLzZt("无");
        k3FlXx.setLx(lx);
        k3FlXx.setShZt(0);
        int i = k3WlXxSQServer.addK3FlXxInseart(k3FlXx);
        if (i > 0) {
            return Result.success("200", "保存成功");
        }
        return Result.failure("500", "保存失败");
    }

    @RequestMapping("/upDate/3FlXx/Update")
    @ResponseBody
    public Result upDateK3FlXxUpdate(@RequestBody JSONObject jsonObject) {
        K3FlXx k3FlXx = jsonObject.getObject("K3FlXx", K3FlXx.class);
        String xgSj = jsonObject.getString("xgSj");
        xgSj = xgSj.replace("/", "-");
        String userName = jsonObject.getString("userName");
        String lx = jsonObject.getString("lx");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        k3FlXx.setLyrGh(userName);
        k3FlXx.setLyrXm(name);
        k3FlXx.setLyrBm(bmmc);
        k3FlXx.setLySj(xgSj);
        k3FlXx.setZt(1);
        k3FlXx.setLzZt("无");
        k3FlXx.setLx(lx);
        k3FlXx.setShZt(0);
        int i = k3WlXxSQServer.upDateK3FlXxUpdate(k3FlXx);
        if (i > 0) {
            return Result.success("200", "保存成功");
        }
        return Result.failure("500", "保存失败");
    }

    @RequestMapping("/select/K3FlXx/DjBh")
    @ResponseBody
    public K3FlXx selectK3FlXxDjBh(@RequestBody JSONObject jsonObject) {
        String djBh = jsonObject.getString("djBh");
        K3FlXx k3FlXx = k3WlXxSQServer.selectK3FlXxDjBh(djBh);
        return k3FlXx;
    }
}
